<template>
  <div>
    <div class="gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>学员</p>
          <el-input v-model="Api_GET_ClubCustomerQO.name" placeholder="请输入学员姓名" size="mini" clearable @change="gettablelist()" />
        </el-col>
        <!-- <el-col :span="4">
          <p>跟进状态</p>
          <el-select v-model="isfollow" size="mini" @change="gettablelist()">
            <el-option label="全部" value="" />
            <el-option v-for="(item, index) in dict.followUpStatus" :key="index" :label="item.value" :value="item.code" />
          </el-select>
        </el-col> -->
        <el-col :span="4">
          <p>录入时间</p>
          <el-date-picker
            v-model="tableDate"
            size="mini"
            style="width: 100%"
            type="daterange"
            range-separator="至"
            start-placeholder="录入开始日期"
            end-placeholder="录入结束日期"
            value-format="yyyy-MM-dd"
            @change="changeDate"
          />
        </el-col>
        <el-col :span="4">
          <p>来源渠道</p>
          <el-select v-model="Api_GET_ClubCustomerQO.CustomerSourceId" size="mini" @change="gettablelist()">
            <el-option label="全部" :value="null" />
            <el-option v-for="item in sourceList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <p>意向度</p>
          <el-select v-model="Api_GET_ClubCustomerQO.IntentionDegreeCode" size="mini" @change="gettablelist()">
            <el-option label="全部" :value="null" />
            <el-option v-for="(item, index) in dict.intentionDegreeType" :key="index" :label="item.value" :value="item.code" />
          </el-select>
        </el-col>
        <!-- <el-col :span="4">
          <p>标签</p>
          <el-select v-model="istitle" size="mini" @change="gettablelist()">
            <el-option label="全部" value="" />
          </el-select>
        </el-col> -->
        <el-col :span="4">
          <p>最后跟进时间</p>
          <el-date-picker
            v-model="followDate"
            size="mini"
            style="width: 100%"
            type="daterange"
            range-separator="至"
            start-placeholder="录入开始日期"
            end-placeholder="录入结束日期"
            value-format="yyyy-MM-dd"
            @change="changefollowDate"
          />
        </el-col>
        <el-col :span="4" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="gettablelist()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="clickReset()">重置</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="nav">
      <div style="margin-bottom:15px">
        <el-radio-group v-model="Api_GET_ClubCustomerQO.FollowUpStatusCode" class="gl_mr_10" size="small" @change="gettablelist()">
          <el-radio-button v-for="item in allocateList" :key="item.code" :label="item.code">
            {{ item.name }}
          </el-radio-button>
        </el-radio-group>
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini" @click="addstudent()">添加客户</el-button>
      </div>

      <el-table v-loading="tableLoading" class="gl_table_noBorder" :data="tableData" size="mini">
        <el-table-column type="index" label="序号" align="center" width="55" />
        <el-table-column label="学员姓名" prop="name" width="80" />
        <el-table-column label="联系方式" prop="phoneNumber" width="100" />
        <el-table-column label="意向度" prop="intentionDegreeType.name" width="60" align="center" />
        <el-table-column label="来源渠道" prop="customerSource.name" min-width="140">
          <template slot-scope="{row}">
            <span v-if="row.customerSource.sourceGroup">{{ row.customerSource.sourceGroup.name + '-' }}</span>
            <span>{{ row.customerSource.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" :min-width="135" align="center">
          <template slot-scope="{row}">
            <span v-if="row.creationTime">
              {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="跟进人" prop="followUpEmployee.name" width="84" />
        <el-table-column label="跟进状态" prop="followUpStatus.name" width="72" />
        <el-table-column label="最后跟进人" prop="lastFollowUpEmployee.name" width="84" />
        <el-table-column label="最后跟进时间" min-width="135" align="center">
          <template slot-scope="{row}">
            <span v-if="row.followUpDate">
              {{ row.followUpDate | FormatDate('yyyy-MM-dd HH:mm:ss') }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="标签" prop="customerTag.name" :min-width="100" />
        <el-table-column label="学员性别" prop="gender.name" align="center" width="72" />
        <!-- <el-table-column label="出生日期" width="85" align="center">
          <template slot-scope="{row}">
            <span v-if="row.creationTime">
              {{ row.creationTime | FormatDate('yyyy-MM-dd') }}
            </span>
          </template>
        </el-table-column> -->
        <el-table-column label="微信" prop="wxAccount" align="center" width="100" />
        <el-table-column label="学员地址" prop="address" align="center" :min-width="140" />
        <el-table-column label="操作" width="100">
          <template slot-scope="{ row }">
            <el-button type="text" size="mini" @click="follow(row)">跟进</el-button>
            <el-button type="text" size="mini" @click="gosign(row)">报名</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="gl_pt_15" style="text-align: right;">
        <el-pagination
          :current-page="Api_GET_ClubCustomerQO.Page"
          :page-size="Api_GET_ClubCustomerQO.MaxResultCount"
          :total="total"
          layout="total,sizes,prev,pager,next,jumper"
          :page-sizes="[10, 20, 30, 40]"
          @size-change="sizeChange"
          @current-change="pageIndexChange"
        />
      </el-row>
      <add-client ref="addClient" :has-follow="false" :employeeid="info.id" @addCustomer="addCustomer" />
    </div>
  </div>
</template>

<script>
import addClient from '../addClient.vue';
import { mapState, mapActions } from 'vuex';
import { Api_GET_ClubCustomerQO, Api_GET_ClubCustomerSourceQO, Api_GET_ClubEmployeeQO } from '@/lib/form/hall/swaggerForm';
import { api_GET_ClubCustomer, api_POST_ClubCustomer, api_GET_ClubCustomerSource, api_GET_ClubEmployee } from '@/extend/api/hallswaggerApi/app';
export default {
  components: { addClient },
  data() {
    return {
      allocateList: [{ code: null, name: '全部' }, { code: 'BeFollowingUp', name: '跟进中' }, { code: 'Subscribe', name: '已邀约' }, { code: 'TrialLesson', name: '已试课' }],
      istitle: '',
      Api_GET_ClubCustomerQO: new Api_GET_ClubCustomerQO(),
      tableLoading: false,
      tableData: [],
      total: 0,
      isfollow: '',
      tableDate: [], // 录入时间
      followDate: [], // 跟进时间
      sourceList: [], // 来源渠道
      EmployeeId: ''
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      cache: state => state.dict.cache,
      info: state => state.user.info
    })
  },
  created() {
    this.getDict();
    this.getUserEmployeeId();
    this.getSource();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict', getDictByType: 'dict/getDictByType' }),
    // 查询我的EmployeeId
    getUserEmployeeId() {
      const that = this;
      const qo = new Api_GET_ClubEmployeeQO();
      qo.UserId = this.info.id;
      qo.IsEnabled = null;
      qo.CanTeach = null;
      this.tableLoading = true;
      api_GET_ClubEmployee(qo).then(res => {
        if (res.items && res.items.length > 0) {
          const _data = res.items[0];
          that.EmployeeId = _data.id;
          that.gettablelist();
        } else {
          this.tableLoading = false;
          this.$message({ type: 'error', message: '获取用户信息失败！' });
        }
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 获取客户列表
    gettablelist(page) {
      this.Api_GET_ClubCustomerQO.Page = page || 1;
      this.Api_GET_ClubCustomerQO.SkipCount = (this.Api_GET_ClubCustomerQO.Page - 1) * this.Api_GET_ClubCustomerQO.MaxResultCount;
      this.Api_GET_ClubCustomerQO.IncludeGender = true;
      this.Api_GET_ClubCustomerQO.IncludeCustomerSource = true;
      this.Api_GET_ClubCustomerQO.IncludeCustomerTag = true;
      this.Api_GET_ClubCustomerQO.IncludeFollowUpStatus = true;
      this.Api_GET_ClubCustomerQO.IncludeFollowUpEmployee = true;
      this.Api_GET_ClubCustomerQO.IncludeLastFollowUpEmployee = true;
      this.Api_GET_ClubCustomerQO.IncludeIntentionDegreeType = true;
      this.Api_GET_ClubCustomerQO.IsCustomerPool = false;
      this.tableLoading = true;
      api_GET_ClubCustomer(this.Api_GET_ClubCustomerQO).then(res => {
        this.tableLoading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 获取来源渠道
    getSource() {
      const qo = new Api_GET_ClubCustomerSourceQO();
      qo.IncludeSourceGroup = true;
      qo.MaxResultCount = 999;
      api_GET_ClubCustomerSource(qo).then(res => {
        this.sourceList = res.items;
      }, error => {
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 切换录入日期
    changeDate (val) {
      const _start = val ? val[0] + ' 00:00:00' : null;
      const _end = val ? val[1] + ' 23:59:59' : null;
      this.Api_GET_ClubCustomerQO.StartDate = _start;
      this.Api_GET_ClubCustomerQO.EndDate = _end;
      this.gettablelist();
    },
    // 切换跟进日期
    changefollowDate(val) {
      const _start = val ? val[0] + ' 00:00:00' : null;
      const _end = val ? val[1] + ' 23:59:59' : null;
      this.Api_GET_ClubCustomerQO.StartFollowUpDate = _start;
      this.Api_GET_ClubCustomerQO.EndFollowUpDate = _end;
      this.gettablelist();
    },
    // 跟进
    follow(row) {
      this.$router.push({ name: 'Follow', query: { id: row.id }});
    },
    addstudent() {
      this.$refs.addClient.dialogVisible = true;
      this.$refs.addClient.customerInput.genderCode = 'M';
    },
    addCustomer(data) {
      this.$refs.addClient.btnLoading = true;
      api_POST_ClubCustomer(data).then(res => {
        this.$refs.addClient.btnLoading = false;
        this.$message({ type: 'success', message: '添加成功' });
        this.$refs.addClient.cancelDialog();
        this.gettablelist();
      }, error => {
        this.$refs.addClient.btnLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 分页
    sizeChange(MaxResultCount) {
      this.Api_GET_ClubCustomerQO.MaxResultCount = MaxResultCount;
      this.gettablelist();
    },
    pageIndexChange(currentPage) {
      this.gettablelist(currentPage);
    },
    // 报名
    gosign(row) {
      console.log(row);
      this.$router.push({
        name: 'Renewal',
        query: {
          add: 'true',
          studentId: row.id,
          studentName: row.name,
          idCardTypeCode: row.idCardTypeCode,
          idCardNumber: row.idCardNumber
        }
      });
      // this.$router.push({ name: 'Renewal', params: { studentId: row.id, studentName: row.name }});
    },
    // 重置
    clickReset() {
      this.Api_GET_ClubCustomerQO = new Api_GET_ClubCustomerQO();
      this.tableDate = [];// 录入时间
      this.followDate = [];// 跟进时间
      this.gettablelist();
    }
  }

};
</script>

<style>

</style>
